﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XRPGLibrary.SpriteClasses;
namespace XRPGLibrary.TileEngine
{
    public enum CameraMode { Free, Follow }
    public class Camera
    {
        #region Field Region
        Vector2 position;//Camera位置
        float speed;//Camera的移动速度
        float zoom;//Camera的放大缩小倍数
        Rectangle viewportRectangle;
        public CameraMode mode;
        #endregion
        #region Property Region
        public Vector2 Position
        {
            get { return position; }
            private set { position = value; }
        }
        public float Speed
        {
            get { return speed; }
            set
            {
                speed = (float)MathHelper.Clamp(speed, 1f, 16f);//该函数的作用是将speed值限定在1和16之间，大于16，返回16，小于1，返回1，在两者之间返回true
            }
        }
        public float Zoom
        {
            get { return zoom; }
        }
        #endregion
        #region Constructor Region
        public Camera(Rectangle viewportRect)//用视野矩阵来初始化相机
        {
            speed = 4f;
            zoom = 1f;
            viewportRectangle = viewportRect;
            mode = CameraMode.Follow;
        }
        public Camera(Rectangle viewportRect, Vector2 position)
        {
            speed = 4f;
            zoom = 1f;
            viewportRectangle = viewportRect;
            mode = CameraMode.Follow;
            Position = position;
        }
        #endregion
        #region Method Region//根据键盘控制移动，实际上将Camera与Player联系在一起
        public void Update(GameTime gameTime)
        {
            if (InputHandler.KeyReleased(Keys.PageUp))
                ZoomIn();//地图缩小
            else if (InputHandler.KeyReleased(Keys.PageDown))
                ZoomOut();//地图放大
            //以上是更新代码
            if (mode == CameraMode.Follow)
                return;
            Vector2 motion = Vector2.Zero;
            if (InputHandler.KeyDown(Keys.Left))
                motion.X = -speed;
            else if (InputHandler.KeyDown(Keys.Right))
                motion.X = speed;
            if (InputHandler.KeyDown(Keys.Up))
                motion.Y = -speed;
            else if (InputHandler.KeyDown(Keys.Down))
                motion.Y = speed;
            if (motion != Vector2.Zero)
            {
                motion.Normalize();
                position += motion * speed;
                LockCamera();
            }
        }
        private void ZoomIn()
        {
            zoom += .25f;
            if (zoom > 2.5f)
                zoom = 2.5f;
        }
        private void ZoomOut()
        {
            zoom -= .25f;
            if (zoom < .5f)
                zoom = .5f;
        }
        private void LockCamera()
        {
             position.X = MathHelper.Clamp(position.X,
            0,
            TileMap.WidthInPixels - viewportRectangle.Width);//将Camera的X坐标限定在0和（地图宽度-Camera视野宽度）之间，特别注意要减去Camera视野宽度，这样就不会出现移出游戏窗体的现象
             position.Y = MathHelper.Clamp(position.Y,
            0,
            TileMap.HeightInPixels - viewportRectangle.Height);
          }

        public void LockToSprite(AnimationSprite sprite)
        {
            position.X = sprite.Position.X + sprite.Width / 2
                - (viewportRectangle.Width / 2);//当精灵移动超过当前Camera视场宽度的一半，Camera的位置才改变，才会水平方向重绘地图
            position.Y = sprite.Position.Y + sprite.Height / 2
                - (viewportRectangle.Height / 2);
            LockCamera();
        }
        public void ToggleCameraMode()
        {
            if (mode == CameraMode.Follow)
                mode = CameraMode.Free;
            else if (mode == CameraMode.Free)
                mode = CameraMode.Follow;
        }
        #endregion
    }
}